package dev.pda.display.presentation.page.util

import dev.pda.display.data.message.entity.ResourceMessage
import dev.pda.display.presentation.page.model.PageData
import dev.pda.display.presentation.page.model.WidgetData
import dev.pda.display.presentation.page.model.WidgetModel
import java.util.*

class WidgetParser {

    fun parseWidget(resourceMessage: ResourceMessage): WidgetData<PageData<WidgetModel<Any>>>? {
        val widgetModels = WidgetData<PageData<WidgetModel<Any>>>()
        val pages: MutableList<PageData<WidgetModel<Any>>> = ArrayList()
        widgetModels.items = pages
        widgetModels.programWidth = resourceMessage.disWidth
        widgetModels.programHeight = resourceMessage.disHeight
        resourceMessage.pages.forEach { page ->
            val pageItem = PageData<WidgetModel<Any>>()
            val widgets: MutableList<WidgetModel<Any>> =
                ArrayList()
            pageItem.widgets = widgets
            pageItem.bg = page.background_image
            pageItem.duration = page.duration
            pageItem.pageNum = page.pageNum

            page.controls.forEach { control ->
                when {
                    control.imageBean != null -> {
                        widgets.add(DataMapper.convertToImageWidget(control) as WidgetModel<Any>)
                    }
                    control.textBean != null -> {
                        widgets.add(DataMapper.convertToTextWidget(control) as WidgetModel<Any>)
                    }
                    control.videoBean != null -> {
                        widgets.add(DataMapper.convertToVideoWidget(control) as WidgetModel<Any>)
                    }
                    control.urlBean != null -> {
                        widgets.add(DataMapper.convertToUrlWidget(control) as WidgetModel<Any>)
                    }
                }
            }
            pages.add(pageItem)
        }
        return widgetModels
    }

}